JDK 8 新特性stream流操作String转换为Map , List 转换为Map,String 转换为List和链式编程 您所在的位置:网站首页 map转list stream JDK 8 新特性stream流操作String转换为Map , List 转换为Map,String 转换为List和链式编程

JDK 8 新特性stream流操作String转换为Map , List 转换为Map,String 转换为List和链式编程

2024-02-26 21:20| 来源: 网络整理| 查看: 265

JDK 8 新特性stream流操作String转换为Map , List 转换为Map,String 转换为List 链式编程 Stream流操作 1.使用字符串将字符串转为list: //要求使用stream流的方式将下面字符串转换为List形式,且按照降序的顺序排列 String str = "12,54,65,87,22,65,48,56,58"; //1.分割字符串 //2.将数组进行流操作 List collect = Arrays.stream(str.split(",")) //类型转换 .map(s -> Integer.valueOf(s)) //降序排序 .sorted(Comparator.comparing(Integer::shortValue).reversed()) //转换为List .collect(Collectors.toList()); System.out.println(collect);

运行结果:

在这里插入图片描述

2.stream流字符串转map: //将下列字符串以map的形式存储key为姓名,value为编号 String strMap = "张三:TX0001_李四:TX002_王五:TX003_赵六:TX004"; //1.对字符进行切割 //2.对流进行操作 Map map = Arrays.stream(strMap.split("_")) .collect(Collectors.toMap(s -> s.split(":")[0], s -> s.split(":")[1])); //将map转换为Json进行打印 System.out.println(JSONArray.toJSONString(map));

运行的结果:

在这里插入图片描述

3.List转map: //使用自己创建的List工具类链式编程添加一些数值 ArrayListUtil list = new ArrayListUtil(); //链式编程 list.addObject("张三:TX0001") .addObject("李四:TX002") .addObject("王五:TX003") .addObject("赵六:TX004"); System.out.println(list); Map map2 = list.stream() .collect(Collectors.toMap(s -> s.split(":")[0], s -> s.split(":")[1])); System.out.println(JSONArray.toJSONString(map2));

运行结果: 在这里插入图片描述

关于stram流操作还有很多以上只是在实际开发较常用的案例

链式编程的好处

拿一些代码来进行对比,集合的普通写法和链式编程之间的区别

普通的代码:

Map map = new HashMap(); map.put("key1","value1"); map.put("key3","value2"); map.put("key4","value3"); map.put("key5","value4"); map.put("key6","value5"); map.put("key7","value6"); map.put("key8","value7"); map.put("key9","value8"); map.put("key10","value9"); map.put("key2","value10"); System.out.println(JSONUtil.parseFromMap(map));

使用map集合将需要的数据插入map中,这样看起来不优雅,而且每次用对象再去put一次两次还好,数多的时候,自己都觉的烦而链式编程的map看下面

HashMapUtil map1 = new HashMapUtil(); map1.putObject("key1", "value1") .putObject("key2", "value2") .putObject("key3", "value3") .putObject("key4", "value4") .putObject("key5", "value5") .putObject("key6", "value6") .putObject("key7", "value7"); System.out.println(JSONUtil.parseFromMap(map1));

而链式编程则看起来优雅很多了而且跟别的业务之间更容易区分,普通的集合类是不支持链式编程的,比如list map,只能是对象add或者对象put,那么就需要自己写一个链式编程的集合工具类去继承这些集合了.

首先说List类:

/** * list链式添加 * @author zhangfc * @date 2021-04-28 14:59:38 * @param */ public class ArrayListUtil extends ArrayList { //重写ArrayList的所有构造函数---start public ArrayListUtil(Collection c) { super(c); } public ArrayListUtil(int initialCapacity) { super(initialCapacity); } public ArrayListUtil() { super(); } //重写ArrayList的所有构造函数---end /** * 对 ArrayList 的 add() 的方法进行封转返回 ArrayListProxy 来实现 链式添加 * @param e * @return */ public ArrayListUtil addObject(E e){ this.add(e); return this; } }

map:

/** * map链式实现 * @author zhangfc * @param * @param */ public class HashMapUtil extends HashMap { //重写HashMap的所有构造函数---start public HashMapUtil(int initialCapacity) { super(initialCapacity); } public HashMapUtil() { super(); } public HashMapUtil(Map m) { super(m); } public HashMapUtil(int initialCapacity, float loadFactor) { super(initialCapacity,loadFactor); } //重写HashMap的所有构造函数---end /** * 对 HashMap 的 put() 的方法进行封转返回 HashMapProxy 来实现 链式添加 * @param key * @param value * @return */ public HashMapUtil putObject(K key, V value){ this.put(key, value); return this; } }

有了这两个工具就可以进行链式编程了: 在这里插入图片描述

当然还有一种做法是使用一个工具框架hutool 里面的工具类也是可以实现链式编程:

导入坐标:

cn.hutool hutool-all 5.5.7

链式编程如下:

Map map2 = MapUtil.builder() .put("key1","value1") .put("key2","value2") .put("key3","value3") .put("key4","value4") .put("key5","value5") .put("key6","value6") .put("key7","value7") .put("key8","value8") .put("key9","value9") .put("key10","value10").build(); System.out.println(JSONUtil.parseFromMap(map2));


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有